package org.maven.dependencybrowser.implementations.local;
/**********************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:

**********************************************************************/
import org.apache.maven.project.interpolation.ModelInterpolationException;
import org.codehaus.plexus.context.Context;
import org.codehaus.plexus.context.ContextException;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Contextualizable;
import org.maven.dependencybrowser.controller.DependencyResolutionException;
import org.maven.dependencybrowser.implementations.Workspace;
import org.maven.dependencybrowser.model.ArtifactInitialisationException;
import org.maven.dependencybrowser.model.PomFile;
import org.maven.dependencybrowser.model.RootPom;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;

public class LocalDiskWorkspace extends Workspace implements Contextualizable
{

    public static final String KEY_WORKSPACE_PATH = "local.workspace";

    private File workspaceDirectory;

    private File rootFile;

    public File getWorkspaceDirectory()
    {
        return this.workspaceDirectory;
    }

    public void loadRootPom(File file) throws ArtifactInitialisationException
    {
        this.rootFile = file;
        reloadPomFile();
    }

    public void reloadPomFile() throws ArtifactInitialisationException
    {
        try
        {
            BufferedReader fileReader = new BufferedReader(new FileReader(rootFile));
            root = new RootPom(loadPom(fileReader), this);
        }
        catch (FileNotFoundException e)
        {
            throw new DependencyResolutionException(e);
        }
        catch (ModelInterpolationException e)
        {
            throw new DependencyResolutionException(e);
        }
    }

    public String getPath(String moduleName)
    {
        return rootFile.getParent() + "/" + moduleName + "/pom.xml";
    }

    public PomFile loadModulePom(String module)
    {
        try
        {
            File file2 = new File(getPath(module));
            BufferedReader fileReader = new BufferedReader(new FileReader(file2));
            return loadPom(fileReader);
        }
        catch (FileNotFoundException e)
        {
            throw new DependencyResolutionException(e);
        }
    }


    public boolean canCreateSnapshot()
    {
        return true;
    }

    public void contextualize(Context context) throws ContextException
    {
        String workspacePath = (String) context.get(KEY_WORKSPACE_PATH);
        if (workspacePath == null || workspacePath.trim().length() == 0)
        {
            throw new ContextException("Missing configuration parameter: " + KEY_WORKSPACE_PATH);
        }
        workspaceDirectory = new File(workspacePath);
    }

}
